﻿using System.Collections.Generic;

namespace Main.lib.space
{

    public sealed class Space<T>
    {

        public  readonly int[] Dimensions;
        private readonly int[] Product;
        public  readonly T[]   Data;

        public Space(params int[] newDimensions)
        {
            Dimensions = newDimensions;
            Product = new int[Dimensions.Length];
            Product[0] = 1;
            int size = Dimensions[0];
            for (int i = 1; i < Dimensions.Length; i++)
            {
                Product[i] = size;
                size *= Dimensions[i];
            }
            Data = new T[size];
        }

        public T this[params int[] position]
        {
            get
            {
                int index = position[0];
                for (int i = 0; i < position.Length; i++)
                    index += position[i] * Product[i];
                return Data[index];
            }
            set
            {
                int index = position[0];
                for (int i = 0; i < position.Length; i++)
                    index += position[i] * Product[i];
                Data[index] = value;
            }
        }

    }

}
